package com.shop.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.shop.dao.DeptMapper;
import com.shop.dao.EmpMapper;
import com.shop.entity.Dept;
import com.shop.entity.Emp;
import com.shop.util.MyBatisUtil;

public class MyBatisTest1 {
	
	
	//测试二级缓存
		@Test
		public void test6() throws IOException{
			SqlSession session = MyBatisUtil.getSqlSession();
			DeptMapper deptMapper = session.getMapper(DeptMapper.class);
			Dept dept = deptMapper.findById(1);
			System.out.println(dept);
			session.close(); //第1个session关闭
			
			SqlSession session2  =MyBatisUtil.getSqlSession(); //第二次获取session
			DeptMapper deptMapper2 = session2.getMapper(DeptMapper.class);
			Dept dept2= deptMapper2.findById(1);
			System.out.println(dept2);
			MyBatisUtil.closesSession(session2);
		}
	
	
	//测试二级缓存
	@Test
	public void test5() throws IOException{
		SqlSession session = MyBatisUtil.getSqlSession();
		EmpMapper empMapper = session.getMapper(EmpMapper.class);
		Emp emp = empMapper.findById(1);
		System.out.println(emp);
		session.close(); //第1个session关闭
		
		SqlSession session2  =MyBatisUtil.getSqlSession(); //第二次获取session
		EmpMapper empMapper2 = session2.getMapper(EmpMapper.class);
		Emp emp2 = empMapper2.findById(1);
		System.out.println(emp2);
		MyBatisUtil.closesSession(session2);
	}
	//============一级缓存测试=================
	
	@Test
	public void test4() throws IOException{
		SqlSession session = MyBatisUtil.getSqlSession();
		EmpMapper empMapper = session.getMapper(EmpMapper.class);
		Emp emp = empMapper.findById(1);
		System.out.println(emp);
		//两次查询之间做增删改
		emp.setEname("古力娜扎");
		empMapper.updateEmp(emp);//调用更新方法
		session.commit();//提交事务
		System.out.println("======第2次查询=======");
		Emp emp2 = empMapper.findById(1);
		System.out.println(emp2);
		MyBatisUtil.closesSession(session);
	}
	
	
	@Test
	public void test3() throws IOException{
		SqlSession session = MyBatisUtil.getSqlSession();
		EmpMapper empMapper = session.getMapper(EmpMapper.class);
		Emp emp = empMapper.findById(1);
		System.out.println(emp);
		session.clearCache();//清空缓存 Map 再次发送
		Emp emp2 = empMapper.findById(1);
		System.out.println(emp2);
		MyBatisUtil.closesSession(session);
	}
	
	
	
	@Test
	public void test2() throws IOException{
		SqlSession session = MyBatisUtil.getSqlSession();
		EmpMapper empMapper = session.getMapper(EmpMapper.class);
		Emp emp = empMapper.findById(1);
		System.out.println(emp);
		session.close(); //第1个session关闭
		
		SqlSession session2  =MyBatisUtil.getSqlSession(); //第二次获取session
		EmpMapper empMapper2 = session2.getMapper(EmpMapper.class);
		Emp emp2 = empMapper2.findById(1);
		System.out.println(emp2);
		MyBatisUtil.closesSession(session2);
	}
	
	
	@Test
	public void test1() throws IOException{
		SqlSession session = MyBatisUtil.getSqlSession();
		EmpMapper empMapper = session.getMapper(EmpMapper.class);
		Emp emp = empMapper.findById(1);
		System.out.println(emp);
		Emp emp2 = empMapper.findById(1);
		System.out.println(emp2);
		MyBatisUtil.closesSession(session);
	}
	
}
